/*
 * @Author: Haochen
 * @Date: 2021-12-07 15:49:55
 * @LastEditTime: 2021-12-13 15:54:18
 * @FilePath: \students-system\src\router\index.js
 */
import Vue from 'vue';
import VueRouter from 'vue-router';
import Home from '../views/home/Home.vue';
// import Login from '../views/login/Login.vue';
const Login = () => import('../views/login/Login.vue');
import Register from '../views/register/Register.vue';
const Error = () => import('../views/404/Error.vue');
// # 导入属于学生的子路由
import StudentsList from '../views/students/StudentsList.vue';
import AddStudent from '../views/students/AddStudent.vue';
const UpdateStudent = () => import('../views/students/UpdateStudent.vue');
// # 导入专业的子路由
const AddSubject = () => import('../views/subjects/AddSubject.vue');
const SubjectsList = () => import('../views/subjects/SubjectsList.vue');
// # 导入老师的子路由
const AddTeacher = () => import('../views/teachers/AddTeacher.vue');
const DeleteTeacher = () => import('../views/teachers/DeleteTeacher.vue');
// # 导入班级的子路由
const AddClass = () => import('../views/classes/AddClass.vue');
const DeleteClass = () => import('../views/classes/DeleteClass.vue');
import ClassesList from '../views/classes/ClassesList';
// # 导入计数器
const Counter = () => import('../components/Counter.vue');

Vue.use(VueRouter);

import { Message } from 'element-ui';

const routes = [
  {
    path: '/',
    redirect: '/home',
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,

    beforeEnter: (to, from, next) => {
      if (localStorage.token) {
        next();
      } else {
        Message({
          message: '请先登录',
          type: 'warning',
        });
        next('/login');
      }
    },
    children: [
      {
        path: 'studentsList',
        name: 'StudentsList',
        component: StudentsList,
        meta: {
          isKeepAlive: false,
          title: '学生管理',
          subtitle: '学生列表',
        },
      },
      {
        path: 'addStudent',
        name: 'AddStudent',
        component: AddStudent,
        meta: {
          isKeepAlive: true,
          title: '学生管理',
          subtitle: '新增学生',
        },
      },
      {
        path: 'updateStudent/',
        name: 'UpdateStudent',
        component: UpdateStudent,
        meta: {
          isKeepAlive: false,
          title: '学生管理',
          subtitle: '修改学生',
        },
      },
      {
        path: 'updateStudent/:id',
        name: 'UpdateStudentWithId',
        component: UpdateStudent,
        meta: {
          isKeepAlive: false,
          title: '学生管理',
          subtitle: '修改学生',
        },
      },
      {
        path: 'addSubject',
        name: 'AddSubject',
        component: AddSubject,
      },
      {
        path: 'subjectsList',
        name: 'SubjectsList',
        component: SubjectsList,
        meta: {
          isKeepAlive: false,
          title: '专业管理',
          subtitle: '专业列表',
        },
      },
      {
        path: 'addTeacher',
        name: 'AddTeacher',
        component: AddTeacher,
      },
      {
        path: 'deleteTeacher',
        name: 'DeleteTeacher',
        component: DeleteTeacher,
      },
      {
        path: 'addClass',
        name: 'AddClass',
        component: AddClass,
      },
      {
        path: 'deleteClass',
        name: 'DeleteClass',
        component: DeleteClass,
      },
      {
        path: 'counter',
        name: 'counter',
        component: Counter,
      },
      {
        path: 'classesList',
        name: 'ClassesList',
        component: ClassesList,
      },
    ],
  },
  {
    path: '/login',
    name: 'Login',
    component: Login,
  },
  {
    path: '/register',
    name: 'Register',
    component: Register,
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ '../views/about/About.vue'),
  },

  {
    path: '*',
    name: 'Error',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: Error,
  },
];

const router = new VueRouter({
  routes,
  mode: 'history',
});

// router.beforeEach((to, from, next) => {
//   // console.log(to);
//   if (/^\/home$/.test(to.path)) {
//     if (localStorage.token) {
//       alert('登录成功');
//       next();
//     } else {
//       alert('未登录');
//       next('/login');
//     }
//   } else {
//     next();
//   }
// });

export default router;
